package com.qiaoba.module.system.controller;

import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.common.base.entity.AjaxResult;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.module.system.entity.SysDept;
import com.qiaoba.module.system.entity.dto.SysDeptDTO;
import com.qiaoba.module.system.entity.param.SysDeptParam;
import com.qiaoba.module.system.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;

/**
 * 部门管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/6 15:06
 */
@Validated
@RestController
@RequestMapping("/system/dept")
@Tag(name = "部门管理")
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public Result<List<SysDept>> list(@Validated SysDeptParam param) {
        return Result.success(sysDeptService.selectList(param));
    }

    @PreAuthorize("hasAuthority('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    @Operation(summary = "获取详情")
    public Result<SysDept> getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String deptId) {
        return Result.success(sysDeptService.selectById(deptId));
    }

    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:dept:add')")
    @PostMapping
    @Operation(summary = "新增部门")
    public Result add(@Validated(AddGroup.class) @RequestBody SysDeptDTO dto) {
        return Result.toAjax(sysDeptService.insert(dto));
    }

    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:dept:edit')")
    @PutMapping
    @Operation(summary = "修改部门")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysDeptDTO dto) {
        return Result.toAjax(sysDeptService.updateById(dto));
    }

    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:dept:remove')")
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门")
    public Result remove(@PathVariable String id) {
        return Result.toAjax(sysDeptService.deleteById(id));
    }

    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/list/exclude/{deptId}")
    @Operation(summary = "查询部门列表(排除节点)")
    public Result<List<SysDept>> excludeChild(@PathVariable(value = "deptId", required = false) String deptId) {
        return Result.success(sysDeptService.excludeChild(deptId));
    }

    @GetMapping("/dept-tree")
    @Operation(summary = "构建部门树")
    public Result deptTree(SysDeptParam param) {
        List<SysDept> list = sysDeptService.selectList(param);
        return Result.success(sysDeptService.buildDeptTree(list));
    }

    @GetMapping(value = "/dept-tree/{roleId}")
    @Operation(summary = "角色部门树")
    public AjaxResult deptTree(@PathVariable("roleId") String roleId, SysDeptParam param) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysDeptService.selectDeptIdsByRoleId(roleId));
        ajax.put("depts", sysDeptService.buildDeptTree(sysDeptService.selectList(param)));
        return ajax;
    }

}
